// LogDiag.cpp : implementation file
//

#include "stdafx.h"
#include "SQLInjectionISAPISettings.h"
#include "LogDiag.h"
#include "FolderDlg.h"
#include "CExclusions.h"


// CLogDiag dialog

IMPLEMENT_DYNAMIC(CLogDiag, CPropertyPage)

CLogDiag::CLogDiag()
	: CPropertyPage(CLogDiag::IDD)
	, enableLog(FALSE)
	, logFolder(_T(""))
	, minimumSeverityID(0)
{

}

CLogDiag::~CLogDiag()
{
}


void CLogDiag::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_CHECK_ENABLE_LOG, enableLog);
	DDX_Text(pDX, IDC_EDIT_LOGFOLDER, logFolder);
	DDV_MaxChars(pDX, logFolder, 256);
	DDX_Control(pDX, IDC_EDIT_LOGFOLDER, textLogFolder);
	DDX_Control(pDX, IDC_SEVERITY, minimumSeverity);
	DDX_CBIndex(pDX, IDC_SEVERITY, minimumSeverityID);
}


BEGIN_MESSAGE_MAP(CLogDiag, CPropertyPage)
	ON_BN_CLICKED(IDC_BUTTON_FOLDER, &CLogDiag::OnBnClickedButtonFolder)
END_MESSAGE_MAP()


// CLogDiag message handlers

void CLogDiag::OnBnClickedButtonFolder()
{
	CFolderDialog folderDlg(_T("Please Choose a Folder"), NULL, NULL, 
		BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE );
	

	if(logFolder.IsEmpty())
	{
		WCHAR tempPath[MAX_PATH] = { 0 };
		DWORD res = GetEnvironmentVariable(_T("ProgramFiles"), (LPWSTR)&tempPath, sizeof(WCHAR)*MAX_PATH);
		ASSERT(res > 0);

		folderDlg.SetSelectedFolder(tempPath);
	} else
	{
		folderDlg.SetSelectedFolder(logFolder.GetBuffer());
		logFolder.ReleaseBuffer();

	}
	if(folderDlg.DoModal() ==  IDOK)
	{
		this->textLogFolder.SetWindowTextW(folderDlg.GetFolderPath());
		logFolder = folderDlg.GetFolderPath();
		//this->UpdateData(TRUE);
		
	}


	/*
	// Change the following for the original folder from registry
	WCHAR tempPath[MAX_PATH] = { 0 };
	DWORD res = GetEnvironmentVariable(_T("ProgramFiles"), (LPWSTR)&tempPath, sizeof(WCHAR)*MAX_PATH);
	ASSERT(res > 0);
	LPITEMIDLIST itemId = new ITEMIDLIST();
	ZeroMemory(itemId, sizeof(ITEMIDLIST));
	HRESULT hr = ::SHParseDisplayName(tempPath, 0, &itemId, 0, 0);
	ASSERT(hr == S_OK);


	
	LPBROWSEINFO bi = new BROWSEINFO();
	ZeroMemory(bi, sizeof(BROWSEINFO));
	ZeroMemory(&tempPath, MAX_PATH*sizeof(TCHAR));
	bi->lpszTitle = _T("Choose log folder");
	bi->hwndOwner = this->GetParent()->GetSafeHwnd();
	bi->ulFlags = BIF_RETURNONLYFSDIRS; // Only folders
	//bi->pidlRoot = itemId;

	bi->pszDisplayName = (LPWSTR)tempPath;
	
	

	//LPBROWSEINFO lpbi = NULL;

	::SHBrowseForFolder(bi);
	logFolder = tempPath;
	//delete &itemId;

	//delete &bi;
	*/
}
